import {
  _decorator,
  Component,
  EventTouch,
  Input,
  input,
  math,
  Node,
  Prefab,
  UITransform,
} from "cc";
import { gameGlobalStatus } from "../GameGlobalStatus";
import { nodePoolManager } from "./NodePoolManager";
import { EnemyPlaneScripts } from "../EnemyPlaneScripts";
import { Constants } from "../Constants";
const { ccclass, property } = _decorator;

@ccclass("UIManager")
export class UIManager extends Component {
  @property(Node)
  heroPlane: Node = null;

  @property(Prefab)
  heroPlaneBullet: Prefab = null;

  @property(Prefab)
  enemyPlane: Prefab = null;

  //初始化射击时间
  _currBulletShootTime: number = 0;

  //创建飞机的时间
  _currEnemyPlaneCreateTime: number = 0;

  _bigEnemyCount: number = 0;
  _middleEnemyCount: number = 0;
  _smallEnemyCount: number = 0;

  onLoad() {
    //初始化当前射击时间
    this._currBulletShootTime = gameGlobalStatus.bulletShootTime;
  }

  start() {
    // 激活监听
    this._setInputEventActive(true);
  }

  /// 创建子弹1-5等级
  _createPlayerBullet(level: number) {
    const airPlanePos = this.heroPlane.position;
    const bullet = nodePoolManager.getNode(this.heroPlaneBullet, this.node);

    bullet.setPosition(
      airPlanePos.x,
      airPlanePos.y + this.heroPlane.getComponent(UITransform).height / 2 + 5
    );
  }

  /// 创建敌机
  _createEnemyPlane() {
    const enemyType = math.randomRangeInt(0, 3);

    switch (enemyType) {
      case Constants.EnemyPlaneType.ENEMY1:
        if (this._smallEnemyCount <= 45) {
          return;
        }
        this._smallEnemyCount = 0;
        break;
      case Constants.EnemyPlaneType.ENEMY2:
        if (this._middleEnemyCount <= 200) {
          return;
        }
        this._middleEnemyCount = 0;
        break;

      case Constants.EnemyPlaneType.ENEMY3:
        if (this._bigEnemyCount <= 500) {
          return;
        }
        this._bigEnemyCount = 0;
        break;
    }

    const enemySpeed = math.randomRangeInt(1, 6);
    const randomPos = math.randomRangeInt(
      -gameGlobalStatus.uiScreenWidth / 2 + 50,
      gameGlobalStatus.uiScreenWidth / 2 - 50
    );
    this.enemyPlane.name = "enemy_" + enemyType + "_" + enemySpeed;
    const enemy = nodePoolManager.getNode(this.enemyPlane, this.node);
    enemy.setPosition(randomPos, gameGlobalStatus.uiScreenHeight);
    const enemyComp = enemy.getComponent(EnemyPlaneScripts);
    enemyComp.init(enemyType, enemySpeed);
  }

  // 激活监听或卸载监听
  _setInputEventActive(active: boolean) {
    if (active) {
      input.on(Input.EventType.TOUCH_MOVE, this._touchMove, this);
    } else {
      input.off(Input.EventType.TOUCH_MOVE, this._touchMove, this);
    }
  }

  _touchMove(event: EventTouch) {
    const _delta = event.getDelta();
    this.heroPlane.setPosition(
      this.heroPlane.position.x + _delta.x,
      this.heroPlane.position.y + _delta.y
    );
  }

  update(deltaTime: number) {
    // 创建飞机的子弹
    this._currBulletShootTime += deltaTime;
    if (this._currBulletShootTime > gameGlobalStatus.bulletShootTime) {
      this._createPlayerBullet(gameGlobalStatus.airPlaneLevel);
      this._currBulletShootTime = 0;
    }

    this._currEnemyPlaneCreateTime += deltaTime;
    this._bigEnemyCount ++;
    this._middleEnemyCount ++;
    this._smallEnemyCount ++;
    if (this._currEnemyPlaneCreateTime > gameGlobalStatus.enemyCreateTime) {
      this._createEnemyPlane();
      this._currEnemyPlaneCreateTime = 0;
    }
  }
}
